package com.itlaoqi.springcloud.memberservice.controller;

import com.itlaoqi.springcloud.memberservice.entity.CallbackResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Controller
public class MemberController {
    @Resource

    private RestTemplate restTemplate;

    @GetMapping("/snb")
    @ResponseBody
    /**
     * 模拟新书到货短信通知客户的业务
     */
    @HystrixCommand(fallbackMethod = "sendNewBookError")
    public String sendNewBook(String mobile,String bookname){
        String message = "[HOTBOOK]您预购的" + bookname + "已到货，明日将送到您府上";
        CallbackResult result = restTemplate.getForObject("http://message-service/sendsms?mobile=" +mobile+"&message=" + message , CallbackResult.class);
        if (result.getCode().equals("0")){
            return "短信已成功送达，服务返回：" +result.getResult();
        }else {
            return "短信发送失败，失败原因:" + result.getResult();
        }
    }
    public String sendNewBookError(String mobile,String bookname){
        return "短信发送失败，失败原因：消息服务无法正常运行，请稍后再试";
    }
}
